package com.alt.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import com.alt.manager.ALTPTCMgr.CONFIRM_DIALOG_ID;
import com.alt.shared.DialogCommand;
import com.alt.R;

public class ALTUserAlert {
	public static final CommandWrapper DISMISS = new CommandWrapper(
			DialogCommand.NO_OP, CONFIRM_DIALOG_ID.NONE, null);

	public ALTUserAlert() {
	}

	public static void showAlert(Context context, int title_id, int message_id,
			DialogInterface.OnClickListener listener) {

		String btnOk = context.getString(R.string.txt_btn_ok);

		(new AlertDialog.Builder(context).setTitle(title_id)
				.setMessage(message_id).setPositiveButton(btnOk, listener)
				.create()).show();
	}

	public static void showAlert(Context context, CharSequence title,
			CharSequence message, DialogInterface.OnClickListener listener) {

		String btnOk = context.getString(R.string.txt_btn_ok);

		(new AlertDialog.Builder(context).setTitle(title).setMessage(message)
				.setPositiveButton(btnOk, listener).setCancelable(false)
				.create()).show();
	}

	public static void showAlert(Context context, int title_id, int message_id,
			int icon_id, DialogInterface.OnClickListener listener) {

		String btnOk = context.getString(R.string.txt_btn_ok);

		(new AlertDialog.Builder(context).setTitle(title_id)
				.setMessage(message_id).setIcon(icon_id)
				.setPositiveButton(btnOk, listener).create()).show();
	}

	public static void showConfirmation(Context context, CharSequence title,
			CharSequence message, final DialogCommand yesCommand,
			CONFIRM_DIALOG_ID dialogId, Object data) {

		String btnOk = context.getString(R.string.txt_btn_ok);
		String btnCancel = context.getString(R.string.txt_btn_cancel);

		new AlertDialog.Builder(context)
				.setCancelable(true)
				.setTitle(title)
				.setMessage(message)
				.setInverseBackgroundForced(true)
				.setPositiveButton(btnOk,
						new CommandWrapper(yesCommand, dialogId, data))
				.setNegativeButton(btnCancel, DISMISS).create().show();
	}

	public static void showConfirmation(Context context, CharSequence title,
			CharSequence message, final DialogCommand yesCommand,
			final DialogCommand noCommand, CONFIRM_DIALOG_ID dialogId,Object data) {

		String btnOk = context.getString(R.string.txt_btn_ok);
		String btnCancel = context.getString(R.string.txt_btn_cancel);

		new AlertDialog.Builder(context)
				.setCancelable(true)
				.setTitle(title)
				.setMessage(message)
				.setInverseBackgroundForced(true)
				.setPositiveButton(btnOk,
						new CommandWrapper(yesCommand, dialogId, data))
				.setNegativeButton(btnCancel,
						new CommandWrapper(noCommand, dialogId, data)).create()
				.show();
	}

	public static class CommandWrapper implements
			DialogInterface.OnClickListener {
		private DialogCommand m_Command;
		private Object m_Data;
		private CONFIRM_DIALOG_ID m_DialogId;

		public CommandWrapper(DialogCommand command,
				CONFIRM_DIALOG_ID dialogId, Object data) {
			this.m_DialogId = dialogId;
			this.m_Command = command;
			this.m_Data = data;
		}

		public void onClick(DialogInterface dialog, int which) {
			dialog.dismiss();
			m_Command.execute(m_DialogId, m_Data);
		}
	}

}
